package com.springapp.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.springapp.jsonmodel.CategoryJson;
import com.springapp.model.Cathegory;
import com.springapp.service.impl.CathegoryServiceImpl;
import com.springapp.service.impl.ItemInOrderServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import com.springapp.util.ControllerUtil;

@Controller
@RequestMapping("/")
public class CathegoryController {

    @Autowired
    private CathegoryServiceImpl csi;
    @Autowired
    private ItemInOrderServiceImpl iiosi;

    /**
     *
     * @author vkrivtc
     */
    @RequestMapping(value = "/category", method = RequestMethod.GET)
    public @ResponseBody
    String getAll() {
        List<Cathegory> catList = csi.getAll();
        List<CategoryJson> json = new ArrayList<>();
        for (int i = 0; i < catList.size(); i++) {
            json.add(CategoryJson.toModel(catList.get(i)));
        }
        Gson gson = new GsonBuilder().create();
        String catJson = gson.toJson(json);
        return ControllerUtil.printSuccess(catJson);
    }

    /**
     *
     * @author vkrivtc
     */
    @RequestMapping(value = "/category/{id}", method = RequestMethod.GET)
    public @ResponseBody
    String getById(@PathVariable int id) {
        Cathegory cat = csi.getById(id);
        if (cat == null) {
            return ControllerUtil.printError();
        }
        CategoryJson json = CategoryJson.toModel(cat);
        Gson gson = new GsonBuilder().create();
        String catJson = gson.toJson(json);
        return ControllerUtil.printSuccess(catJson);
    }

    /*
     * 
     * @uthor Sim4ik
     * 
     */
    @RequestMapping(value = "/category/{id}", method = RequestMethod.DELETE)
    public @ResponseBody
    String delete(@PathVariable("id") String id) {
        Cathegory cat = csi.getById(Integer.valueOf(id));
        if (cat == null) {
            return ControllerUtil.printError();
        }
        CategoryJson json = CategoryJson.toModel(cat);
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        String categoryJson = gson.toJson(json);
        csi.delete(Integer.valueOf(id));
        return ControllerUtil.printSuccess(categoryJson);
    }

    /*
     * 
     * @uthor Sim4ik
     * 
     */
    @RequestMapping(value = "/category", method = RequestMethod.POST,
    consumes = "application/json")
    public @ResponseBody
    String update(@RequestBody String json) {
        Gson gson = new Gson();
        CategoryJson cj = gson.fromJson(json, CategoryJson.class);
        Cathegory cathegory = new Cathegory();
        cathegory.setName(cj.getName());
        csi.create(cathegory);
        return ControllerUtil.printSuccess(json);
    }

    /*
     * 
     * @uthor Sim4ik
     * 
     */
    @RequestMapping(value = "/category/{id}", method = RequestMethod.PUT,
    consumes = "application/json")
    public @ResponseBody
    String add(@RequestBody String json, @PathVariable("id") int id) {
        Gson gson = new Gson();
        CategoryJson cj = gson.fromJson(json, CategoryJson.class);
        Cathegory cathegory = csi.getById(id);
        if (cathegory == null) {
            return ControllerUtil.printError();
        }
        cathegory.setName(cj.getName());
        csi.update(cathegory);
        return ControllerUtil.printSuccess(json);
    }
}
